package com.zz.bid.service;

import com.mysql.cj.util.StringUtils;
import com.zz.bid.entity.Group;
import com.zz.bid.exception.GroupNotExistException;
import com.zz.bid.exception.base.BidException;
import com.zz.bid.mapper.GroupMapper;
import com.zz.bid.to.GroupTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class GroupService {

    @Autowired
    private GroupMapper groupMapper;


    //TODO 结果放入缓存
    public List<GroupTO> getGroups() throws BidException {
        try {
            List<Group> groups = groupMapper.getGroups();
            if (null != groups) {
                return groups.stream().map(group -> {
                    //将字符串转化为整型数组
                    Integer[] access;
                    if (null != group.getOperation()) {
                        String[] accessStrs = group.getOperation().split(",");

                        access = new Integer[accessStrs.length];
                        for (int i = 0; i < accessStrs.length; i++) {
                            access[i] = Integer.parseInt(accessStrs[i]);
                        }
                    } else {
                        access = new Integer[0];
                    }
                    return GroupTO.builder().name(group.getName()).operation(access).id(group.getId()).build();
                }).collect(Collectors.toList());
            }
            return new ArrayList<>();
        } catch (Exception e) {
            throw new BidException(e.getMessage());
        }
    }

    @Transactional
    public void setGroupAccess(Integer groupId, Integer[] addedAccess, Integer[] removedAccess) throws BidException {
        try {
            Group group = groupMapper.getGroup(groupId);
            if (group == null) {
                throw new GroupNotExistException();
            }

            ArrayList<Integer> newOperations = new ArrayList<>(Arrays.asList(convertGroupAccess(group.getOperation())));
            newOperations.removeAll(Arrays.asList(removedAccess));
            newOperations.addAll(Arrays.asList(addedAccess));
            groupMapper.setGroupAccess(group.getId(), convertToOperationString(newOperations.toArray(new Integer[0])));
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BidException(e.getMessage());
        }
    }

    public void addGroup(String groupName) throws BidException {
        try {
            groupMapper.addGroup(groupName);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BidException(e.getMessage());
        }
    }

    private Integer[] convertGroupAccess(String access) {
        if (StringUtils.isNullOrEmpty(access)) {
            return new Integer[0];
        }
        String[] accessStrs = access.split(",");
        Integer[] accessInts = new Integer[accessStrs.length];
        for (int i = 0; i < accessStrs.length; i++) {
            accessInts[i] = Integer.parseInt(accessStrs[i]);
        }
        return accessInts;
    }

    private String convertToOperationString(Integer[] accessInts) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < accessInts.length; i++) {
            stringBuilder.append(accessInts[i]);
            if (i != accessInts.length - 1) {
                stringBuilder.append(",");
            }
        }
        return stringBuilder.toString();
    }
}
